package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.query.TagQueryObject;
import cn.wolfcode.trip.base.service.*;
import cn.wolfcode.trip.base.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//攻略资源控制器
@RestController
@RequestMapping("strategies")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyCatalogService strategyCatalogService;

    @Autowired
    private IStrategyDetailService strategyDetailService;
    @Autowired
    private ITravelService         travelService;
    @Autowired
    private ITagService            tagService;

    @GetMapping
    public PageInfo query(StrategyQueryObject qo){
        PageInfo<?> pageInfo = strategyService.query(qo);
        return pageInfo;
    }

    /**
     * 根据id获取大攻略对象
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Strategy getById(@PathVariable Long id){
        Strategy strategy = strategyService.getById(id);
        return strategy;
    }

    /**
     * 根据大攻略id查询攻略分类集合
     * @param id
     * @return
     */
    @GetMapping("/{id}/catalogs")
    public List<StrategyCatalog> listCatalogByStrategyId(@PathVariable Long id){
        return strategyCatalogService.listByStrategyId(id);
    }

    /**
     * 根据攻略文章的id查询文章内容
     * @param id
     * @return
     */
    @GetMapping("/details/{id}")
    public StrategyDetail getDetailsById(@PathVariable Long id){
        return strategyDetailService.getDetailsById(id);
    }

    //大攻略搜索接口
    @GetMapping("/searchByKeyWord")
    public Object getByKeyWord(@RequestParam String keyword){
        JsonResult jsonResult = new JsonResult();
        StrategyQueryObject qo = new StrategyQueryObject();
        qo.setKeyword(keyword);
        try {
            Strategy query = strategyService.selectStrategyByKeyWord(qo);
            jsonResult.setResult(query);
            return jsonResult;
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark("服务器异常");
            return jsonResult;
        }
    }
    //主页搜索接口,查询分页数据
    @GetMapping("/selectBykeyword")
    public Object selectBykeyword(StrategyQueryObject qo){
        JsonResult jsonResult = new JsonResult();
        PageInfo<?> list  = strategyService.selectListByKeyword(qo);
        return list;
    }

    /**
     * 根据攻略ID查询 所关联游记
     * @param strategyId
     * @return
     */
    @GetMapping("/{strategyId}/travels")
    public List<Travel>  getTravelsBystrategyId(@PathVariable Long strategyId){
        return travelService.listTravelByStategyIdForApp(strategyId);
    }

    //拿攻略ID查询对应所有标签
    @GetMapping("/{strategyId}/tags")
    @ResponseBody
    public Object  getBystrategycommentId(@PathVariable("strategyId") Long strategyId){
        TagQueryObject qo = new TagQueryObject();
        qo.setStrategyId(strategyId);
        //设置重复次数,当重复次数大于此数字时,标签才会显示出来
        qo.setNum(2);
        List<Tag> list  = tagService.selectByStrategyId(qo);
        return list;
    }

}
